/**
 * @file network.h
 * @brief Defines basic constants and types that are related to networking.
 * @author Vamshi Ponnapalli
 */

#ifndef _NETWORK_H
  #define _NETWORK_H
  /*****************************************************************************
   * Includes
   ****************************************************************************/
  #include "hal_types.h"

  #ifdef __cplusplus
    extern "C" {
  #endif
  /*****************************************************************************
   * Types
   ****************************************************************************/   
  /**
   * Represents a 48-bit MAC address.
   */
  typedef struct 
  {
    uint8 b1;
    uint8 b2;
    uint8 b3;
    uint8 b4;
    uint8 b5;
    uint8 b6;
  } mac_address_t;
  
  /**
   * Represents a 32-bit IP address.
   */
  typedef struct 
  {
    uint8 b1;
    uint8 b2;
    uint8 b3;
    uint8 b4;
  } ip_address_t;
  
  /**
   * Represents a UDP header.
   */
  typedef struct
  {
    ip_address_t destIPAddress;
    uint16 destPort;
    uint16 dataSize;
  } udp_header_t;
    
  
  /**
   * Represents a UDP message.
   */
  typedef struct
  {
    udp_header_t header;
    uint8 * data;
  } udp_message_t; 
  /*****************************************************************************
   * Constants
   ****************************************************************************/
  //Minimum size of TCP header.
  #define NETWORK_TCP_MIN_HDR_SIZE    20
  
  //Maximum size of TCP header
  #define NETWORK_TCP_MAX_HDR_SIZE    60
  
  /*****************************************************************************
   * Function declarations
   ****************************************************************************/
  /**
   * Reads a 2 byte word from a buffer in network byte order.
   * 
   * @param pBuffer A reference to the buffer containing the data.
   * 
   * @return The 2 byte word read from the buffer.
   */
  uint16 network_readWord(uint8 *pBuffer);
  
  /**
   * Reads a 4 byte DWord from a buffer in network byte order.
   * 
   * @param pBuffer A reference to the buffer containing the data.
   * 
   * @return The 4 byte DWord read from the buffer.
   */
  uint32 network_readDWord(uint8 *pBuffer);
  
  /**
   * Writes a 2 byte word to a buffer in network byte order.
   * 
   * @param word The 2 byte word to be written to the buffer.
   * @param pBuffer A reference to the buffer to which the data will be written.
   */
  void network_writeWord(uint16 word, uint8 *pBuffer);
  
  /**
   * Writes a 4 byte DWord to a buffer in network byte order.
   * 
   * @param dword The 4 byte dword to be written to the buffer.
   * @param pBuffer A reference to the buffer to which the data will be written.
   */
  void network_writeDWord(uint32 dword, uint8 *pBuffer);
  
  /**
   * Converts a 32 bit number into an IP address format.
   *
   * @param address the 32 bit number that represents the IP address.
   *
   * @return The IP address in the ip_address_t structure.
   */
  ip_address_t network_intToIPAddress(uint32 address);

  /**
   * Converts an IP address format to a 32 bit number.
   *
   * @param ipAddress The IP address in the ip_address_t structure.
   *
   * @return The IP address represented as a 32 bit number.
   */
  uint32 network_ipAddressToInt(ip_address_t ipAddress);  
  #ifdef __cplusplus
    }
  #endif

#endif